// pages/person/login/index.js
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        is_user: {
            // 接收参数的类型
            type: String,
            // 接收参数的默认值
            value: "",
            // 为0则不显示
            observer(e) {
                if (e == 1) {
                    this.setData({
                        loginModalVisible: true,
                        showPrivacy: true,
                        loginWXVisible: true,
                        loginMobileVisible: false,
                    });
                } else if (e == 2) {
                    this.setData({
                        loginModalVisible: true,
                        showPrivacy: true,
                        loginMobileVisible: true,
                        loginWXVisible: false,
                    });
                }
            },
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        loginModalVisible: false,
        loginWXVisible: false,
        loginMobileVisible: false,
        mobile: "",
        password: "",
        userInfo: [],
        showPrivacy: false,
        privacyContractName: '',
    },
    pageLifetimes: {
      show() {

        if (wx.getPrivacySetting) {
          wx.getPrivacySetting()
        } else {
          // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
          wx.showModal({
            title: '提示',
            content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
          })
          return
        }

          const _ = this
          wx.getPrivacySetting({
              success(res) {
                  if (res.errMsg == "getPrivacySetting:ok") {
                      _.setData({
                          privacyContractName: res.privacyContractName,
                          showPrivacy: res.needAuthorization
                      })
                  }
              }
          })
      }
  },
    /**
     * 组件的方法列表
     */

    methods: {
      openPrivacyContract() {
        const _ = this
        wx.openPrivacyContract({
            fail: () => {
                wx.showToast({
                    title: '遇到错误',
                    icon: 'error'
                })
            }
        })
    },
         // 拒绝隐私协议
         exitMiniProgram() {
          // 直接退出小程序
          wx.exitMiniProgram()
      },
      // 同意隐私协议
      handleAgreePrivacyAuthorization() {
          const _ = this
          _.setData({
              showPrivacy: false
          })
 
      },

        close() {
            this.triggerEvent("is_user_show", true);
            this.setData({
                loginModalVisible: false,
                is_user: 0,
            });
        },
 
        getUserInfo: function (e) {
            const that = this;
            console.log(e);
            console.log("获取用户信息成功", e.detail.userInfo);
            wx.login({
                success: function (res) {
                    app.http.post(app.api.getOpenid, {
                        code: res.code
                    }, (reso) => {
                      console.log('openid'+reso);  
                        app.http.post(app.api.threelogin, {
                            Sharestoreid: wx.getStorageSync('Sharestoreid'),
                            Shareuid: wx.getStorageSync('Shareuid'),
                            openid: reso.data.info.openid,
                            nickName: e.detail.userInfo.nickName,
                            gender: e.detail.userInfo.gender,
                            avatarUrl: e.detail.userInfo.avatarUrl
                        }, (res) => {
                            wx.setStorageSync("uid", res.data.res.member_id);
                            wx.setStorageSync("token", res.data.res.token);
                            wx.setStorageSync("openid", reso.data.info.openid);
                            that.setData({
                              loginWXVisible: false,
                          });
                        })
                    })
                },
            });
        },
    },
});